package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zcx.helper.http.AsyCallBack;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import longcaisuyun.longcai.com.myview.MyListView;
import longcaisuyun.longcai.com.net.PostFuWu;
import longcaisuyun.longcai.com.suyunbean.FuWuBean;


public class FuWuActiviyy extends Activity {

    List<FuWuBean> list = new ArrayList<FuWuBean>();
    LinearLayout fanhui_lin;
    MyListView mylist;
    TextView tv_content;
    TextView fuwu_biaoti;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fu_wu_activiyy);
        View view = getWindow().getDecorView();
        // 适配工具将视图重新排布
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        ViewInit();
        httpget();
    }

    private void ViewInit() {
        fuwu_biaoti = (TextView) findViewById(R.id.tv_fuwu_biaoti);
        tv_content = (TextView) findViewById(R.id.tv_content);
        fanhui_lin = (LinearLayout) findViewById(R.id.fanhui_lin);
        mylist = (MyListView) findViewById(R.id.mylist);
        mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

                fuwu_biaoti.setText("     " + list.get(position).getTitle());
                tv_content.setText("     " + list.get(position).getContent());


            }
        });
        LinearLayout fanhui_lin = (LinearLayout) findViewById(R.id.fanhui_lin);
        fanhui_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void httpget() {
        MyApplication.myviewutil.startProgressDialog(FuWuActiviyy.this);
        new PostFuWu(new AsyCallBack<PostFuWu.Info>() {

            public void onSuccess(String toast, int type, PostFuWu.Info info) throws Exception {
                super.onSuccess(toast, type, info);

                if (info.message.equals("0")) {
                    Toast.makeText(FuWuActiviyy.this, "获取数据失败", Toast.LENGTH_SHORT).show();
                } else {
                    JSONArray jsonArray = info.jsonArray;
                    tolist(jsonArray);
                    mylist.setAdapter(new FuWuAdapter(FuWuActiviyy.this, list));

                }
                MyApplication.myviewutil.stopProgressDialog();
            }


            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }
        }).execute(FuWuActiviyy.this);
    }

    public void tolist(JSONArray jsonArray) {
        for (int j = 0; j < jsonArray.length(); j++) {

            try {
                FuWuBean fuWuBean = new FuWuBean();
                JSONObject jsonObject = (JSONObject) jsonArray.get(j);
                fuWuBean.setContent(jsonObject.getString("content"));
                fuWuBean.setId(jsonObject.getString("id"));
                fuWuBean.setTitle(jsonObject.getString("title"));
                list.add(fuWuBean);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }
}
